package com.likuicat.picture_ddd.interfaces.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.likuicat.picture_ddd.infrastructure.annotation.AuthCheck;
import com.likuicat.picture_ddd.infrastructure.common.BaseResponse;
import com.likuicat.picture_ddd.infrastructure.common.DeleteRequest;
import com.likuicat.picture_ddd.infrastructure.common.ResultUtils;
import com.likuicat.picture_ddd.interfaces.assembler.UserAssembler;
import com.likuicat.picture_ddd.interfaces.dto.user.*;
import com.likuicat.picture_ddd.domain.user.constant.UserConstant;
import com.likuicat.picture_ddd.infrastructure.exception.ErrorCode;
import com.likuicat.picture_ddd.infrastructure.exception.ThrowUtils;
import com.likuicat.picture_ddd.domain.user.entity.User;
import com.likuicat.picture_ddd.interfaces.vo.user.LoginUserVO;
import com.likuicat.picture_ddd.interfaces.vo.user.UserVO;
import com.likuicat.picture_ddd.application.service.UserApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    UserApplicationService userApplicationService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.NOT_FOUND_ERROR);
        long userId = userApplicationService.userRegister(userRegisterRequest);
        return ResultUtils.success(userId);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.NOT_FOUND_ERROR);
        LoginUserVO loginUserVO = userApplicationService.userLogin(userLoginRequest, request);
        return ResultUtils.success(loginUserVO);
    }

    @ApiOperation("获取登录用户信息")
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userApplicationService.getLoginUser(request);
        return ResultUtils.success(userApplicationService.getUserToLoginUserVO(loginUser));
    }

    @ApiOperation("用户注销")
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        boolean logout = userApplicationService.userLogout(request);
        return ResultUtils.success(logout);
    }

    /**
     * 【管理员】创建用户
     * @param userAddRequest
     * @return
     */
    @ApiOperation("添加用户")
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)// 切面，仅管理员角色可创建用户
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.NOT_FOUND_ERROR);
        User userEntity = UserAssembler.toUserEntity(userAddRequest);
        Long userId = userApplicationService.saveUser(userEntity);
        return ResultUtils.success(userId);
    }

    /**
     * 【管理员】根据 id 删除用户
     * @param deleteRequest
     * @return
     */
    @ApiOperation("删除用户")
    @GetMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)// 切面，仅管理员角色使用该接口
    public BaseResponse<Boolean> deleteUser(DeleteRequest deleteRequest) {
        boolean removed = userApplicationService.deleteUser(deleteRequest);
        return ResultUtils.success(removed);
    }

    /**
     * 【管理员】更新用户
     * @param updateRequest
     * @return
     */
    @ApiOperation("更新用户")
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)// 切面，仅管理员角色可创建用户
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest updateRequest) {
        ThrowUtils.throwIf(updateRequest == null, ErrorCode.PARAMS_ERROR);
        User userEntity = UserAssembler.toUserEntity(updateRequest);
        userApplicationService.updateUser(userEntity);
        return ResultUtils.success(true);
    }

    /**
     * 【管理员】分页获取用户列表（需要脱敏）
     * @param userQueryRequest
     * @return
     */
    @ApiOperation("分页获取用户列表（需要脱敏）")
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)// 切面，仅管理员角色可创建用户
    public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest userQueryRequest) {
        Page<UserVO> userVOPage = userApplicationService.listUserVOByPage(userQueryRequest);
        return ResultUtils.success(userVOPage);
    }

    /**
     * 【管理员】根据 id 获取用户（未脱敏）
     * @param userId
     * @return
     */
    @ApiOperation("根据 id 获取用户（未脱敏）")
    @GetMapping("/get")
    public BaseResponse<User> getUser(long userId) {
        User user = userApplicationService.getUserById(userId);
        return ResultUtils.success(user);
    }

    /**
     * 【管理员】根据 id 获取用户（脱敏）
     * @param userId
     * @return
     */
    @ApiOperation("根据 id 获取用户（脱敏）")
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVO(long userId) {
        User user = userApplicationService.getUserById(userId);
        UserVO userVO = userApplicationService.getUserToUserVO(user);
        return ResultUtils.success(userVO);
    }
}
